//
//  BirthdayRelatedInfo.swift
//  FearfulNotification
//
//  Created by 筱鹏 on 2019/7/5.
//  Copyright © 2019 筱鹏. All rights reserved.
//

import UIKit

struct BirthdayRelatedInfo {
    private static let BirthdayIdUserDefaultKey = "BirthdayIdUserDefaultKey"
    private static var birthdayId:Int64 = 0;
    
    private static let BirthdayDemoGenerateKey = "BirthdayDemoGenerateKey"
    
    static func obtainBirthdayId() -> Int64 {
        let numberOptional = UserDefaults.standard.value(forKey: BirthdayIdUserDefaultKey)
        if let number = (numberOptional as? NSNumber) {
            birthdayId = number.int64Value
        }
        fearfulPrint("birthdayId = \(birthdayId)")
        return birthdayId;
    }
    
    static func updateBirthdayId() {
        fearfulPrint("birthdayId = \(birthdayId + 1)")
        UserDefaults.standard.set(NSNumber(value: birthdayId + 1), forKey: BirthdayIdUserDefaultKey)
        UserDefaults.standard.synchronize()
    }
    
    static func generateDemoData() {
        let exist = UserDefaults.standard.bool(forKey: BirthdayDemoGenerateKey)
        if exist {
            return
        }
        let birthdayId = self.obtainBirthdayId()
        BirthdayEntity.execute.insert({ (finish) in
            UserDefaults.standard.set(true, forKey: BirthdayDemoGenerateKey)
            UserDefaults.standard.synchronize()
            self.updateBirthdayId()
            NotificationCenter.default.post(name: .AddNewBirthday, object: nil)
        }) { (entity) in
            entity.alert = false
            entity.birthdayId = birthdayId
            entity.birthdayMonth = 1
            entity.birthdayDay = 1
            entity.name = "刘忻"
            entity.desc = "🎂"
        }
    }

}
